package com.example.administrator.gpsstationbletool.comm;

import android.content.Context;
import android.content.SharedPreferences;



/**
 * Created by Administrator on 2018/7/13.
 */

public class gpsparam {
    public   String wifiPasswd = "120.78.154.66";
    public   String wifiName = "HF00176";
    public   String serverIp = "";
    public int serverPort = 0;
    public float baseLon;
    public float baseLat;
    public float baseAlt;
    public float  baseCutoff;

    private  Context context;

    public gpsparam(Context context)
    {
        this.context = context;
    }

    public boolean loadParamFromini()
    {
        SharedPreferences read =  this.context.getSharedPreferences("GPSParamSet", 0);
        if (read == null) return  false;
        //步骤2：获取文件中的值
        wifiName = read.getString("wifiName", "YGCX");
        wifiPasswd = read.getString("wifiPasswd", "ygcx888888");
        serverIp = read.getString("serverIp", "192.168.1.135");
        serverPort =  read.getInt("serverPort", 5051);
        baseLon = read.getFloat("baseLon", 106);
        baseLat = read.getFloat("baseLat", 25);
        baseAlt = read.getFloat("baseAlt", 100);
        baseCutoff = read.getFloat("baseCutoff", 10);


        return  true;
    }

    public boolean saveParam()
    {
        SharedPreferences.Editor editor = this.context.getSharedPreferences("GPSParamSet", 0).edit();
        if (editor == null) return  false;

        editor.putString("wifiName", wifiName);
        editor.putString("wifiPasswd", wifiPasswd);
        editor.putString("serverIp", serverIp);
        editor.putInt("serverPort", serverPort );
        editor.putFloat("baseLon", baseLon);
        editor.putFloat("baseLat", baseLat);
        editor.putFloat("baseAlt", baseAlt);
        editor.putFloat("baseCutoff", baseCutoff);

        editor.commit();
        editor.apply();
        return  true;
    }

    public boolean saveParam(String id,Object value,String type)
    {
        SharedPreferences.Editor editor = this.context.getSharedPreferences("GPSParamSet", 0).edit();
        if (editor == null) return  false;

        if(type == "s")
            editor.putString(id, (String) value);
        else if(type == "d")
            editor.putInt(id, (int) value);
        else if(type == "f")
            editor.putFloat(id, (float) value);
        else if(type == "b")
            editor.putBoolean(id, (boolean) value);

        editor.commit();
        editor.apply();
        return  true;
    }
}
